package com.jy.rhin.support.interceptor.convertor;

import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.oasis_open.docs.wsn.b_2.Unsubscribe;
import org.oasis_open.docs.wsn.b_2.UnsubscribeResponse;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service("AuditInfoConverter4TerminologyDocumentUnSubscribe")
public class AuditInfoConverter4TerminologyDocumentUnSubscribe extends BaseAuditInfoConverter<Unsubscribe, UnsubscribeResponse> {

    public AuditInfoConverter4TerminologyDocumentUnSubscribe() {
        super(EventActionCodeEnum.EXECUTE,
                ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH,
                EventTypeCodeEnum.DOCUMENT_UNSUBSCRIBE,
                true,
                ParticipantObjectIDTypeCodeEnum.URI,
                ParticipantObjectTypeCodeRoleEnum.SUBSCRIBER,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     *
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("documents-subandpub-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(Unsubscribe unsubscribe, UnsubscribeResponse unsubscribeResponse) {
        return new AuditParticipantUserDesc("client-2", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(Unsubscribe unsubscribe, UnsubscribeResponse unsubscribeResponse) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(Unsubscribe unsubscribe, UnsubscribeResponse unsubscribeResponse) {
        return new AuditParticipantUserDesc("documents-subandpub-service", ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH.getName());
    }

    @Override
    String getObjectId(Unsubscribe unsubscribe, UnsubscribeResponse unsubscribeResponse) {
        if (Objects.nonNull(unsubscribe)) {
            return unsubscribe.getSubscriptionId();
        }
        if (Objects.nonNull(unsubscribeResponse)) {
            if (Objects.nonNull(unsubscribeResponse.getFault())) {
                return unsubscribeResponse.getFault();
            }
            if (Objects.nonNull(unsubscribeResponse.getSuccessMessge())) {
                return unsubscribeResponse.getSuccessMessge();
            }
        }
        return "";
    }
    @Override
	public boolean isMatch(String method, Object data) {
		return "DocumentSubAndPubPortImpl.documentUnSubscribe".equals(method);
	}
    
}

